var fieldJobId;
var changeFromClick = false;
// var reBackNodeText;
// 初始化数据
var parameterData = [ {
	valueCode : '0',
	valueName : '默认参数'
}, {
	valueCode : '1',
	valueName : '自定义参数'
} ];
// 定位显示
var arrayLi = [];
var arrayLiInd = 0;
var deleteParamIds ="";

var resultSysNum = 0;
var loadSysNum = 0;
$(document).ready(
				function() {
					$('#comboxReqParameterType').combobox({
						url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_PARAMETER_TYPE + '&v=' + Math.random(),
						valueField: 'valueCode',
					    textField: 'valueName',
					    editable:false,
					    method:'get',
					    loadFilter:function(data){
					    	return data;
					    },
					    onLoadSuccess:function(){
					    	var reqParameterType = $('#comboxReqParameterType').combobox('getData');
					    	$("#comboxReqParameterType").combobox('setValue',reqParameterType[0].valueCode);
					    }
					});
					
					
					$('#reviseComboxReqParameterType').combobox({
						url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_PARAMETER_TYPE + '&v=' + Math.random(),
						valueField: 'valueCode',
					    textField: 'valueName',
					    editable:false,
					    method:'get',
					    loadFilter:function(data){
					    	return data;
					    },
					    onLoadSuccess:function(){
					    	var reqParameterType = $('#reviseComboxReqParameterType').combobox('getData');
					    	$("#reviseComboxReqParameterType").combobox('setValue',reqParameterType[0].valueCode);
					    }
					});
					
					$('#reviseComboxRepParameterType').combobox({
						url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_PARAMETER_TYPE + '&v=' + Math.random(),
						valueField: 'valueCode',
					    textField: 'valueName',
					    editable:false,
					    method:'get',
					    loadFilter:function(data){
					    	return data;
					    },
					    onLoadSuccess:function(){
					    	var reqParameterType = $('#reviseComboxRepParameterType').combobox('getData');
					    	$("#reviseComboxRepParameterType").combobox('setValue',reqParameterType[0].valueCode);
					    }
					});
					
					$('#comboxRepParameterType').combobox({
						url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_PARAMETER_TYPE + '&v=' + Math.random(),
						valueField: 'valueCode',
					    textField: 'valueName',
					    editable:false,
					    method:'get',
					    loadFilter:function(data){
					    	return data;
					    },
					    onLoadSuccess:function(){
					    	var repParameterType = $('#comboxRepParameterType').combobox('getData');
					    	$("#comboxRepParameterType").combobox('setValue',repParameterType[0].valueCode);
					    }
					});
					
					
					$(".right_con").css("display", "block");
					
					// 领域系统结构树
					var tree_id = '';
					var isSave = false;
					var modifyNode;

					// var reBackNode;
					$("#tt")
							.tree(
									{
										url : BASE_URL_TREE
												+ '/findInterfacesForMsgByUser'
												+ '?v=' + Math.random(),
										lines : false,
										onLoadSuccess : function(node, data) {
											$("#tt > li > ul > li > .tree-node > .tree-file").css("background","url('../../../images/st_folder.png') 2px 2px no-repeat");
											var nodes = $('#tt').tree('getChildren');
											function changeNodeClass(nodelist, start, end) {
												if(start < nodelist.length)
												{
													for(var i = start; i <= end; i++)
													{
														var node = nodelist[i];
														if(node.nodeType == "fieldSystem" && node.hasChildren && (!node.children || node.children.length == 0) && $(node.target).find(".tree-fieldSys").length == 0 )
														{
															$(node.target).find("span.tree-icon").prev().remove();
															$(node.target).children(".tree-icon.tree-file").before('<span class="tree-fieldSys tree-collapsed" data-id="' + node.id + '"></span>');
															$(node.target).children(".tree-fieldSys").click(function(){
																var $self = $(this);
																if($self.hasClass('tree-collapsed'))
																{//alert(node.id+'  '+node.text);													
																	$self.removeClass('tree-collapsed').addClass('tree-expanded');
																	//$('#tt').tree('expend', $self.parent()[0])
																	$.ajax({
																		url :BASE_URL_TREE+ '/getHttpBySystemIdForMsgScan?systemId=' + $self.attr("data-id"),
																		dataType : 'json',
																		type : 'get',
																		success : function(data){
																			//alert(data)
																			$('#tt').tree('append',{
																				parent: $self.parent()[0],
																				data: data
																			});
																		}
																	}); 
																}
																else
																{
																	$self.addClass('tree-collapsed').removeClass('tree-expanded');
																	$('#tt').tree('collapse', $self.parent()[0]);
																}
																
															});
														}
														var icons = $(node.target).find("span.tree-icon");
														if (node.nodeType == 'systemInterface') {
															//var icons = $(node.target).find("span.tree-icon");
															if(node.interfaceType=='1'){
																$(icons[0]).removeClass("tree-file");
																$(icons[0]).addClass("icons4WebService");
															}
															else if(node.interfaceType=='2'){
																$(icons[0]).removeClass("tree-file");
																$(icons[0]).addClass("icons4Rest");
															}
															else if(node.interfaceType=='3'){
																$(icons[0]).removeClass("tree-file");
																$(icons[0]).addClass("icons4Ftp");
															}
														}
													}
													if(end + 1 < nodelist.length)
													{
														start = end + 1;
														end += 100;
														end = end > nodelist.length - 1? nodelist.length - 1:end;
														setTimeout(function(){
															changeNodeClass(nodelist, start, end);
														}, 50);
													}
												}
											}
											var end = nodes.length > 100? 99:nodes.length;
											changeNodeClass(nodes, 0, end);
										},
										onBeforeSelect : function(node) {
											if ($(node.target).children(
													".tree-folder").length) {
												return false;
											}
											if ($(node.target).children(
													".tree-file").length
													&& ($(node.target).parent()
															.parent().parent()
															.parent()[0].id == "tt" || $(
															node.target)
															.parent().parent()[0].id == "tt")) {
												return false;
											}
											return true;
										},
										onClick : function(node) {
											arrayLi = [];
											arrayLiInd = 0;
											changeFromClick = true;
											$("#systemName").val(node.text);
										},
										onSelect : function(node) {
											/** ***************************************************** */
											$("#ff").data("changed", false);
											if (node.nodeType == 'systemInterface') {
												// $(".right_mask").css("display","none");
												fieldJobId = node.id;
												if (tree_id != fieldJobId) {
													tree_id = fieldJobId;
													$("#ff input").val("");
												} else {
													return false;
												}
												var interfaceId;
												$.ajax({
															url : BASE_URL_HTTP_INTERFACE + '/findIfByIdForMsg' + '?id=' + node.id + '&v=' + Math.random(),
															dataType : 'json',
															type : 'get',
//															contentType : "application/json",
															success : function(data) {
																if(data.backValue == true){
																	FormEcho(data);
																}else{
																	$.messager.alert("提示","<p>FTP接口没有报文！</p>","info");
																}
																
															},
															error : function() {
																$.messager
																		.alert(
																				"错误",
																				"<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>",
																				"error");
															}

														});

											}
											/** ********************************************************************************* */

										}
									});

					

$("#searchNode").click(function(e) {
	if ($("#systemName").val() != '') {
		//后台查询匹配的接口
		$.ajax({
			url : BASE_URL_HTTP_INTERFACE + '/findHttpInterfaceByIfName' + '?ifName=' + encodeURI($("#systemName").val()) + '&v=' + Math.random(),
			dataType : 'json',
			type : 'get',
			success : function(data) {
				if(data.backValue == true){
					var system = data.httpDatas;
					if(system.indexOf(",") != -1){
						var systems = system.split(",");
					}else{
						var systems = [system];
					}
					resultSysNum = systems.length;
					loadSysNum = 0;
					for(var i=0; i < systems.length; i++){
						//展开系统的节点
						//找到树的所有节点
						var nodes = $('#tt').tree('getChildren');
						for(var j = 0; j < nodes.length; j++)
						{
							if(nodes[j].text == systems[i]){
								var $self = $(nodes[j].target).find("span.tree-fieldSys");
								if($self.length)
								{	
									 expendNode(nodes[j]);
								}
								else
								{
									loadSysNum++;
									if(loadSysNum >= resultSysNum)
									{
										choose_tree();
									}
								}
								break;
							}
						}
						
					}
				
				}else{
					$.messager.alert("错误","<p>没有找到包含  " + $("#systemName").val() + " 的接口！ </p>","error");
				}
			},
			error : function() {
				$.messager.alert("错误","<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>","error");
			}
		});
	} else {
		// 搜索关键字为空时，不进行搜索
		/*
		 * $("input").val(""); var row = $('#tt li .tree-node');
		 * row.removeClass('tree-node-selected');
		 */
		$(".tree_con").scrollTop(0);
	}
});
$("#systemName").bind('input propertychange', function() {
	if (changeFromClick) {
		changeFromClick = false;
		return;
	}
	arrayLi = [];
	arrayLiInd = 0;
	resultSysNum = 0;
	loadSysNum = 0;
	if ($("#systemName").val() != '') {
		//choose_tree();
	} else { // 搜索关键字为空时，不进行搜索
		/*
		 * $("#ff input").val(""); var row = $('#tt li .tree-node');
		 * row.removeClass('tree-node-selected');
		 */
		$(".tree_con").scrollTop(0);
	}
});


function expendNode(node) {
	var $self = $(node.target).find("span.tree-collapsed");
	if(!$self.length)
	{
		return;
	}
	$self.removeClass('tree-collapsed').addClass('tree-expanded');
	$.ajax({
		url :BASE_URL_TREE+ '/getHttpBySystemIdForMsgScan?systemId=' + node.id,
		dataType : 'json',
		type : 'get',
		success : function(data){
			//alert(data)
			$('#tt').tree('append',{
				parent: $self.parent()[0],
				data: data
			});
			loadSysNum++;
			if(loadSysNum >= resultSysNum)
			{
				choose_tree();
			}
		},
		error: function() {
			loadSysNum++;
			if(loadSysNum >= resultSysNum)
			{
				choose_tree();
			}
		}
	}); 
}
$(document, "#systemName").keyup(function(event) {
	if (event.keyCode == 13) {
		$("#searchNode").trigger("click");
	}
});

function getSelected() {
	var node = $('#tt').tree('getSelected');
	if (node.nodeType == 'systemInterface') {
		$.ajax({
			url : BASE_URL_BASE_URL_HTTP_INTERFACE_CRUD + '/' + node.id + '?v='
					+ Math.random(),
			dataType : 'json',
			type : 'get',
			contentType : "application/json",
			success : function(data) {
				FormEcho(data);
			},
			error : function() {
				alert("error");
			}

		});
	}
}

function choose_tree() {
	$('#tt').tree('expandAll');
	if (arrayLi.length == 0 && arrayLiInd == 0) {
		var searchVal = $("#systemName").val();
		var row = $('#tt li .tree-node');
		var rowcolumn = $('#tt li .tree-node > .tree-title');
		var i = 0, j = 0;

		// row.removeClass('tree-node-selected');
		row.removeClass('tree-node-hover');
		rowcolumn.each(function(column) {
			var matchVal = $(this).text();
			if (matchVal.indexOf(searchVal) != -1) {
				i++;
				var scrTop = $(".tree_con").scrollTop() + $(this).offset().top
						- parseInt($(".tree_con").css("padding-top"), 10)
						- $(".search-con")[0].offsetHeight
						- $(".left_menu > .menu_tit")[0].offsetHeight;

				if (i <= 1) {
					// $(this).parent().addClass('tree-node-selected');
					$(this).parent().addClass('tree-node-hover'); // 改成鼠标hover状态，不选中搜索结果

					$(".tree_con").scrollTop(scrTop);
					arrayLiInd = 1;
					// getSelected();
				}
				arrayLi.push([ matchVal, scrTop ]);
			}
		});
	} else {
		var rowcolumn = $('#tt li .tree-node > .tree-title');
		var row = $('#tt li .tree-node');
		// row.removeClass('tree-node-selected');
		row.removeClass('tree-node-hover');
		if (arrayLiInd < arrayLi.length) {
			rowcolumn.each(function(column) {
				var matchVal = $(this).text();
				if (matchVal == arrayLi[arrayLiInd][0]) {
					// $(this).parent().addClass('tree-node-selected');
					$(this).parent().addClass('tree-node-hover'); // 改成鼠标hover状态，不选中搜索结果
					$(".tree_con").scrollTop(arrayLi[arrayLiInd][1]);
					arrayLiInd++;
					// getSelected();
					return false;
				}
			});
		} else {
			// arrayLi = [];
			arrayLiInd = 0;
			$(".tree_con").scrollTop(0);
			choose_tree();
		}
	}
}

//动态添加接口请求参数数据项
$(".confirm_title").click(function(e) {
	if($("#requestParam").form("validate")){
		var reqParamId = $("#reqParamId").val();
        var reqParameterName = $("#reqParameterName").val();
		var reqParameterType = $("#comboxReqParameterType").combobox('getValue');
		var reqParameterTypeDispaly = $("#comboxReqParameterType").combobox('getText');
		var reqParameterIsMust = $("#reqParameterIsMust").is(":checked");
		var reqParameterIsMustValue;
		//alert($("#reqParameterIsMust").is(":checked"));
		
		var reqParameterIsMustDispaly;
		if(reqParameterIsMust == true){
			reqParameterIsMustValue = "1";
			reqParameterIsMustDispaly = "是";
		}else{
			reqParameterIsMustValue = "0";
			reqParameterIsMustDispaly = "否";
		}
		var reqParameterDefaultValue = $("#reqParameterDefaultValue").val();
		var reqParameterDes = $("#reqParameterDes").val();
		if(reqParameterName != '' && reqParameterType !== '')
		{
			var tableHtml = '<tr id="rev_index' + Math.floor(Math.random()*100000) + '"><td width="20%" style="display:none" class="reqParamId">' + reqParamId + '</td><td width="16.7%" class="reqParameterName"><p class="txtP" title="' + reqParameterName + '">' + reqParameterName + '</p></td><td width="16.7%" class="comboxReqParameterType" data-val="' + reqParameterType + '"><p class="txtP" title="' + reqParameterTypeDispaly + '">' + reqParameterTypeDispaly + '</p></td><td width="16.7%" class="reqParameterIsMust" data-val="' + reqParameterIsMustValue + '"><p class="txtP" title="' + reqParameterIsMustDispaly + '">' + reqParameterIsMustDispaly + '</p></td><td width="16.7%" class="reqParameterDefaultValue"><p class="txtP" title="' + reqParameterDefaultValue + '">' + reqParameterDefaultValue + '</p></td><td width="16.7%" class="reqParameterDes"><p class="txtP" title="' + reqParameterDes + '">' + reqParameterDes + '</p></td><td width="16.7%" class="operation_table"><a href="javascript:void(0);" class="change_title">更改</a><a href="javascript:void(0);" class="del_title">删除</a></td></tr>';
			$(".add").find("tr").eq(0).after(tableHtml);
			$("#reqParamId, #reqParameterName, #comboxReqParameterType, #reqParameterIsMust, #reqParameterDefaultValue, #reqParameterDes").val('');
		}else
		{
			$.messager.alert('警告', '<p>请填写完整！</p>', 'warning');
		}
	}
	else
	{
		$(".interfaceDataAdd .validatebox-invalid").eq(0).focus();
	}
});


//修改接口请求参数数据项
$('#interface_change').window({
	width:650,
	height:140,
	modal:true,
	minimizable:false,
	maximizable:false,
	collapsible:false
});
$('#interface_change').window('close');  //关闭窗口
var rev_index;
$("body").delegate(".change_title","click",function(e) {
	$("#reviseParamId, #reviseReqParameterName, #reviseComboxReqParameterType, #reviseReqParameterIsMust, #reviseReqParameterDefaultValue, #reviseReqParameterDes").val("");
	rev_index = $(this).parents("tr").attr("id");
	var reqParamId = $(this).parents("td").siblings(".reqParamId").text();
	var reqParameterName = $(this).parents("td").siblings(".reqParameterName").text();
	var reqParameterType = $(this).parents("td").siblings(".comboxReqParameterType").attr("data-val");
	var reqParameterDes = $(this).parents("td").siblings(".reqParameterDes").text();
	var reqParameterDefaultValue = $(this).parents("td").siblings(".reqParameterDefaultValue").text();
	var reqParameterIsMust = $(this).parents("td").siblings(".reqParameterIsMust").attr("data-val");
	var html;
	
	$("#reviseParamId").val(reqParamId);
	$("#reviseReqParameterName").val(reqParameterName);
	$("#reviseComboxReqParameterType").combobox('select',reqParameterType);
	if(reqParameterIsMust == "1"){
		$("#reviseReqParameterIsMust")[0].checked = true;
	}else{
		$("#reviseReqParameterIsMust")[0].checked = false;
	}
	$("#reviseReqParameterDefaultValue").val(reqParameterDefaultValue);
	$("#reviseReqParameterDes").val(reqParameterDes);
	$('#interface_change').window('open');	
	return false;
});

$("body").delegate(".confirm_title_revise","click",function(e) {
	if($("#requestReviseParam").form("validate")){
		$("#" + rev_index).children(".reqParamId").text($("#reviseParamId").val());
		$("#" + rev_index).children(".reqParameterName").html('<p class="txtP" title="' + $("#reviseReqParameterName").val() + '">' + $("#reviseReqParameterName").val() + '</p>');
		$("#" + rev_index).children(".comboxReqParameterType").attr("data-val",$("#reviseComboxReqParameterType").combobox('getValue'));
		$("#" + rev_index).children(".comboxReqParameterType").html('<p class="txtP" title="' + $("#reviseComboxReqParameterType").combobox('getText') + '">' + $("#reviseComboxReqParameterType").combobox('getText') + '</p>');
		var reviseReqParameterIsMust = $("#reviseReqParameterIsMust").is(":checked");
		var reviseReqParameterIsMustValue;
		if(reviseReqParameterIsMust == true){
			reviseReqParameterIsMustValue = "1";
			reviseReqParameterIsMustDispaly = "是";
		}else{
			reviseReqParameterIsMustValue = "0";
			reviseReqParameterIsMustDispaly = "否";
		}
		
		$("#" + rev_index).children(".reqParameterIsMust").attr("data-val",reviseReqParameterIsMustValue);
		$("#" + rev_index).children(".reqParameterIsMust").html('<p class="txtP" title="' + reviseReqParameterIsMustDispaly + '">' + reviseReqParameterIsMustDispaly + '</p>');
		$("#" + rev_index).children(".reqParameterDefaultValue").html('<p class="txtP" title="' + $("#reviseReqParameterDefaultValue").val() + '">' + $("#reviseReqParameterDefaultValue").val() + '</p>');
		$("#" + rev_index).children(".reqParameterDes").html('<p class="txtP" title="' + $("#reviseReqParameterDes").val() + '">' + $("#reviseReqParameterDes").val() + '</p>');
		$('#interface_change').window('close');	
	}else{
		$(".interfaceDataAdd .validatebox-invalid").eq(0).focus();
	}
});

//删除接口数据项
$("body").delegate(".del_title","click",function(){
	$(this).parents("tr").remove();
	var paramId = $(this).parents("tr").children("td").eq(0).text();
	deleteParamIds += (deleteParamIds == ""? "":",") + paramId;
});



 //动态添加接口返回参数数据项
$(".confirm_title_rep").click(function(e) {
	if($("#responseParam").form("validate")){
		var repParamId = $("#repParamId").val();
        var repParameterName = $("#repParameterName").val();
		var repParameterType = $("#comboxRepParameterType").combobox('getValue');
		var repParameterTypeDispaly = $("#comboxRepParameterType").combobox('getText');
		var repParameterDes = $("#repParameterDes").val();
		if(repParameterName != '' && repParameterType !== '')
		{
			var tableHtml = '<tr id="rev_index' + Math.floor(Math.random()*100000) + '"><td width="20%" style="display:none" class="repParamId">' + repParamId + '</td><td width="25%" class="repParameterName"><p class="txtPP" title="' + repParameterName + '">' + repParameterName + '</p></td><td width="25%" class="comboxRepParameterType" data-val="' + repParameterType + '"><p class="txtPP" title="' + repParameterTypeDispaly + '">' + repParameterTypeDispaly + '</p></td><td width="25%" class="repParameterDes"><p class="txtPP" title="' + repParameterDes + '">' + repParameterDes + '</p></td><td width="25%" class="operation_table"><a href="javascript:void(0);" class="change_title_rep">更改</a><a style="margin-left:10px;" href="javascript:void(0);" class="del_title_rep">删除</a></td></tr>';
			$(".add_rep").find("tr").eq(0).after(tableHtml);
			$("#repParamId, #repParameterName, #comboxRepParameterType, #repParameterDes").val('');
		}else
		{
			$.messager.alert('警告', '<p>请填写完整！</p>', 'warning');
		}
	}
	else
	{
		$(".interfaceDataAdd_rep .validatebox-invalid").eq(0).focus();
	}
});


//修改接口返回参数数据项
$('#interface_change_rep').window({
	width:555,
	height:140,
	modal:true,
	minimizable:false,
	maximizable:false,
	collapsible:false
});
$('#interface_change_rep').window('close');  //关闭窗口
var rev_index;
$("body").delegate(".change_title_rep","click",function(e) {
	$("#reviseRepParamId, #reviseRepParameterName, #reviseComboxRepParameterType, #reviseRepParameterDes").val("");
	rev_index = $(this).parents("tr").attr("id");
	var repParamId = $(this).parents("td").siblings(".repParamId").text();
	var repParameterName = $(this).parents("td").siblings(".repParameterName").text();
	var repParameterType = $(this).parents("td").siblings(".comboxRepParameterType").attr("data-val");
	var repParameterDes = $(this).parents("td").siblings(".repParameterDes").text();
	var html;
	
	$("#reviseRepParamId").val(repParamId);
	$("#reviseRepParameterName").val(repParameterName);
	$("#reviseComboxRepParameterType").combobox('select',repParameterType);
	$("#reviseRepParameterDes").val(repParameterDes);
	$('#interface_change_rep').window('open');	
	return false;
});

$("body").delegate(".confirm_title_revise_rep","click",function(e) {
	if($("#responseReviseParam").form("validate")){
		$("#" + rev_index).children(".repParamId").text($("#reviseRepParamId").val());
		$("#" + rev_index).children(".repParameterName").html('<p class="txtPP" title="' + $("#reviseRepParameterName").val() + '">' + $("#reviseRepParameterName").val() + '</p>');
		$("#" + rev_index).children(".comboxRepParameterType").attr("data-val",$("#reviseComboxRepParameterType").combobox('getValue'));
		$("#" + rev_index).children(".comboxRepParameterType").html('<p class="txtPP" title="' + $("#reviseComboxRepParameterType").combobox('getText') + '">' + $("#reviseComboxRepParameterType").combobox('getText') + '</p>');
		$("#" + rev_index).children(".repParameterDes").html('<p class="txtPP" title="' + $("#reviseRepParameterDes").val() + '">' + $("#reviseRepParameterDes").val() + '</p>');
		$('#interface_change_rep').window('close');	
	}else{
		$(".interfaceDataAdd .validatebox-invalid").eq(0).focus();
	}
});


//删除接口返回数据项
$("body").delegate(".del_title_rep","click",function(){
	$(this).parents("tr").remove();
	var paramId = $(this).parents("tr").children("td").eq(0).text();
	deleteParamIds += (deleteParamIds == ""? "":",") + paramId;
});







});


//接口请求数据项数据转JSON
var columnDefinitions;
function interfaceData_req()
{
	var table_con = $(".add tbody").children().clone();
	$(".interfaceData tbody").html('<tr><th width="20%">reqParamId</th><th width="20%">reqParameterName</th><th width="20%">comboxReqParameterType</th><th width="20%">reqParameterIsMust</th><th width="20%">reqParameterDefaultValue</th><th width="20%">reqParameterDes</th></tr>');
	$(".interfaceData tbody").append(table_con);
	$(".interfaceData tr:eq(1)").remove();
	for(var i = 1, len = $(".interfaceData tr").length; i < len; i++)
	{
		$(".interfaceData tr:eq(" + i + ") td:eq(2)").text($(".interfaceData tr:eq(" + i + ") td:eq(2)").attr("data-val"));
		$(".interfaceData tr:eq(" + i + ") td:eq(3)").text($(".interfaceData tr:eq(" + i + ") td:eq(3)").attr("data-val"));
	}		
	$(".interfaceData tr td:last-child").remove();
	columnDefinitions = $('.interfaceData').tableToJSON(); 
	//alert(JSON.stringify(columnDefinitions));
	return columnDefinitions;
}


//接口返回数据项数据转JSON
var columnDefinitions_rep;
function interfaceData_rep()
{
	var table_con = $(".add_rep tbody").children().clone();
	$(".interfaceData_rep tbody").html('<tr><th width="20%">repParamId</th><th width="20%">repParameterName</th><th width="20%">comboxRepParameterType</th><th width="20%">repParameterDes</th></tr>');
	$(".interfaceData_rep tbody").append(table_con);
	$(".interfaceData_rep tr:eq(1)").remove();
	for(var i = 1, len = $(".interfaceData_rep tr").length; i < len; i++)
	{
		$(".interfaceData_rep tr:eq(" + i + ") td:eq(2)").text($(".interfaceData_rep tr:eq(" + i + ") td:eq(2)").attr("data-val"));
	}		
	$(".interfaceData_rep tr td:last-child").remove();
	columnDefinitions_rep = $('.interfaceData_rep').tableToJSON(); 
	return columnDefinitions_rep;
}




//更新接口报文
function updateIfMsg(){
	var interfaceId = $("#interfaceId").val();
	var interfaceName = $("#interfaceName").val();
	var requestParameters  = interfaceData_req();
	var requestParametersMsg = $("#requestParametersMsg").val();
	
	
	var responseParameters = interfaceData_rep();
	var responesParametersMsg = $("#responesParametersMsg").val();
	
	
	var interfaceData = {
			id:interfaceId,
			requestParameters:requestParameters,
			requestMess:requestParametersMsg,
			responseParameters:responseParameters,
			returnMess:responesParametersMsg
	}
	
	if(deleteParamIds != ""){
		if(deleteParamIds.indexOf(",") != -1){
			interfaceData["deleteParamIds"] = deleteParamIds == ""?[]:deleteParamIds.split(",");
		}else{
			interfaceData["deleteParamIds"] = [deleteParamIds];
		}
	}else{
		interfaceData["deleteParamIds"] = []
	}
	
	/*var interfaceDatas = JSON.stringify(interfaceData);*/
	var jsonData = {
		interfaceDatas:interfaceData
	};
	
	$("#progressbar, #progressbar_tips").fadeIn();
	loading_point.timer = setInterval(loading,200);
	$.ajax({
			type: "post",
			url: BASE_URL_HTTP_INTERFACE + '/updateIfMessage' + '?v=' + Math.random(),
			dataType:'json',
			data: JSON.stringify(jsonData),
			contentType:"application/json",
			success: function(result) {
				if(result.backValue === true){
						setTimeout(function(){
							$("#systemName").val(window.decodeURI(interfaceName));
							$("#searchNode").click();
							$("#progressbar, #progressbar_tips").fadeOut();
							clearInterval(loading_point.timer);
							$.messager.alert('提示','<p>报文更新成功!</p>', 'info');
						}, 3000);
						updateSuccess(interfaceId);
				}else if(result.backValue === false){
					$("#progressbar, #progressbar_tips").fadeOut();
					clearInterval(loading_point.timer);
					$.messager.alert('错误','<p>' + result.errorDes + '!</p>', 'error');
				}else{
					$("#progressbar, #progressbar_tips").fadeOut();
					clearInterval(loading_point.timer);
					$.messager.alert('错误','<p>报文更新失败!</p>', 'error');
				}
				
			},
			error: function() {
				$("#progressbar, #progressbar_tips").fadeOut();
				clearInterval(loading_point.timer);
				$.messager.alert('错误','<p>服务异常，修改报文管理失败，请稍后重试或联系管理员!</p>', 'error');
			}
	 });
}


function updateSuccess(interfaceId){
	$.ajax({
				url : BASE_URL_HTTP_INTERFACE + '/findIfByIdForMsg' + '?id=' + interfaceId + '&v=' + Math.random(),
				dataType : 'json',
				type : 'get',
//				contentType : "application/json",
				success : function(data) {
					if(data.backValue == true){
						FormEcho(data);
					}else{
						$.messager.alert("提示","<p>FTP接口没有报文！</p>","info");
					}
					
				},
				error : function() {
					$.messager
							.alert(
									"错误",
									"<p>服务异常，查询接口信息失败，请重新登录或稍后再试！ </p>",
									"error");
				}

			});
}


//表单数据回显
function FormEcho(data) {
		deleteParamIds = "";
		if(data.type == "3"){
			$.messager.alert("错误","<p>FTP接口没有报文！</p>","error");
		}else{
			
			var reqRows = $(".add").find("tr").size();
			for(var i = reqRows; i >　1; i--){
				$(".add").find("tr").eq(i - 1).remove();
			}
			
			var repRows = $(".add_rep").find("tr").size();
			for(var j = repRows; j >　1; j--){
				$(".add_rep").find("tr").eq(j - 1).remove();
			}
			
			if(data.type == "1"){
				$("#requestParamMsg").css("display", "block");
			}else{
				$("#requestParamMsg").css("display", "none");
			}
			$("#interfaceId").val(data.id);
			$("#interfaceInfo").text(data.name + "-接口基本信息");
			$("#interfaceName").val(data.name);
			if(data.requestMess != null && data.requestMess != ""){
				$("#requestParametersMsg").text(data.requestMess.replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
			}else{
				$("#requestParametersMsg").text("");
			}
			if(data.returnMess != null && data.returnMess != ""){
				$("#responesParametersMsg").text(data.returnMess.replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
			}else{
				$("#responesParametersMsg").text("");
			}
			
			for(var i = 0; i < data.interfaceParameter.length; i++){
					//alert("result" + JSON.stringify(result.interfaceParameter[0]));
					//alert(result.interfaceParameter[i].type);
					if(data.interfaceParameter[i].paramType == "1"){
  					if($(".interfaceDataAdd").form("validate")){
  						var ParamId = data.interfaceParameter[i].paramId;
  			            var reqParameterName = data.interfaceParameter[i].paramName;
  						var reqParameterType = data.interfaceParameter[i].paramDataType;
  						var reqParameterTypeDispaly;
  						if(reqParameterType == "0"){
  							reqParameterTypeDispaly = "String";
  						}else if(reqParameterType == "1"){
  							reqParameterTypeDispaly = "double";
  						}else if(reqParameterType == "2"){
  							reqParameterTypeDispaly = "int";
  						}else if(reqParameterType == "3"){
  							reqParameterTypeDispaly = "boolean";
  						}else if(reqParameterType == "4"){
  							reqParameterTypeDispaly = "byte";
  						}else if(reqParameterType == "5"){
  							reqParameterTypeDispaly = "short";
  						}else if(reqParameterType == "6"){
  							reqParameterTypeDispaly = "long";
  						}else if(reqParameterType == "7"){
  							reqParameterTypeDispaly = "float";
  						}else if(reqParameterType == "8"){
  							reqParameterTypeDispaly = "JSON";
  						}else if(reqParameterType == "9"){
  							reqParameterTypeDispaly = "object";
  						}else if(reqParameterType == "10"){
  							reqParameterTypeDispaly = "File";
  						}
  						
  						
  						var reqParameterIsMust = data.interfaceParameter[i].isNeed;
  						var reqParameterIsMustValue;
  						
  						var reqParameterIsMustDispaly;
  						if(reqParameterIsMust == "1"){
  							reqParameterIsMustValue = "1";
  							reqParameterIsMustDispaly = "是";
  						}else{
  							reqParameterIsMustValue = "0";
  							reqParameterIsMustDispaly = "否";
  						}
  						var reqParameterDefaultValue = data.interfaceParameter[i].paramDefaultValue;
  						var reqParameterDes = data.interfaceParameter[i].paramDes;
  						if(reqParameterName != '' && reqParameterType !== '')
  						{
  							var tableHtml = '<tr id="rev_index' + Math.floor(Math.random()*100000) + '"><td width="20%" style="display:none" class="ParamId">' + ParamId + '</td><td width="16.7%" class="reqParameterName"><p class="txtP" title="' + reqParameterName + '">' + reqParameterName + '</p></td><td width="16.7%" class="comboxReqParameterType" data-val="' + reqParameterType + '"><p class="txtP" title="' + reqParameterTypeDispaly + '">' + reqParameterTypeDispaly + '</p></td><td width="16.7%" class="reqParameterIsMust" data-val="' + reqParameterIsMustValue + '"><p class="txtP" title="' + reqParameterIsMustDispaly + '">' + reqParameterIsMustDispaly + '</p></td><td width="16.7%" class="reqParameterDefaultValue"><p class="txtP" title="' + reqParameterDefaultValue + '">' + reqParameterDefaultValue + '</p></td><td width="16.7%" class="reqParameterDes"><p class="txtP" title="' + reqParameterDes + '">' + reqParameterDes + '</p></td><td width="16.7%" class="operation_table"><a href="javascript:void(0);" class="change_title">更改</a><a href="javascript:void(0);" class="del_title">删除</a></td></tr>';
  							$(".add").find("tr").eq(0).after(tableHtml);
  							$("#reqParamId, #reqParameterName, #comboxReqParameterType, #reqParameterIsMust, #reqParameterDes").val('');
  						}else
  						{
  							$.messager.alert('警告', '<p>请填写完整！</p>', 'warning');
  						}
  					}
  					else
  					{
  						$(".interfaceDataAdd .validatebox-invalid").eq(0).focus();
  					}
					
					}else{
						if($(".responseParametersForm").form("validate")){
							var repParamId = data.interfaceParameter[i].paramId;
				            var repParameterName = data.interfaceParameter[i].paramName;
				            
							var repParameterType = data.interfaceParameter[i].paramDataType;
							var repParameterTypeDispaly;
							
							if(repParameterType == "0"){
								repParameterTypeDispaly = "String";
  						}else if(repParameterType == "1"){
  							repParameterTypeDispaly = "double";
  						}else if(repParameterType == "2"){
  							repParameterTypeDispaly = "int";
  						}else if(repParameterType == "3"){
  							repParameterTypeDispaly = "boolean";
  						}else if(repParameterType == "4"){
  							repParameterTypeDispaly = "byte";
  						}else if(repParameterType == "5"){
  							repParameterTypeDispaly = "short";
  						}else if(repParameterType == "6"){
  							repParameterTypeDispaly = "long";
  						}else if(repParameterType == "7"){
  							repParameterTypeDispaly = "float";
  						}else if(repParameterType == "8"){
  							repParameterTypeDispaly = "JSON";
  						}else if(repParameterType == "9"){
  							repParameterTypeDispaly = "object";
  						}else if(repParameterType == "10"){
  							repParameterTypeDispaly = "File";
  						}
							
							
							var repParameterDes = data.interfaceParameter[i].paramDes;
							if(repParameterName != '' && repParameterType !== '')
							{
								var tableHtml = '<tr id="rev_index' + Math.floor(Math.random()*100000) + '"><td width="20%" style="display:none" class="repParamId">' + repParamId + '</td><td width="25%" class="repParameterName"><p class="txtPP" title="' + repParameterName + '">' + repParameterName + '</p></td><td width="25%" class="comboxRepParameterType" data-val="' + repParameterType + '"><p class="txtPP" title="' + repParameterTypeDispaly + '">' + repParameterTypeDispaly + '</p></td><td width="25%" class="repParameterDes"><p class="txtPP" title="' + repParameterDes + '">' + repParameterDes + '</p></td><td width="25%" class="operation_table"><a href="javascript:void(0);" class="change_title_rep">更改</a><a style="margin-left:10px;" href="javascript:void(0);" class="del_title_rep">删除</a></td></tr>';
								$(".add_rep").find("tr").eq(0).after(tableHtml);
								$("#repParamId, #repParameterName, #comboxRepParameterType, #repParameterDes").val('');
							}else
							{
								$.messager.alert('警告', '<p>请填写完整！</p>', 'warning');
							}
						}
						else
						{
							$(".interfaceDataAdd_rep .validatebox-invalid").eq(0).focus();
						}
					}
		}
}
}


var loading_point = {
		timer:null,
		len:3,
		count:0
	};
function loading()
{
 	if(loading_point.count < loading_point.len)
	{
		loading_point.count++;
		$("#progressbar_point").text($("#progressbar_point").text() + ".");
	}else
	{
		loading_point.count = 0;
		$("#progressbar_point").text("");
	}	
}



